package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.common.utils.MyPageHelper;
import com.project.entity.Class;
import com.project.entity.Department;
import com.project.entity.Major;
import com.project.myBeans.R;
import com.project.service.ClassService;
import com.project.service.DepartmentService;
import com.project.service.MajorService;
import com.project.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 16:15
 * @param:
 * @return:
 */
@EnableAutoConfiguration
@RestController
@RequestMapping("/department")
@Api(description = "系部接口", value = "department")
public class DepartmentController extends BaseService{
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private ClassService classService;

    /**
     * TateBrown 2018年5月26日
     *
     * @param schoolId 学院id
     * @return
     */
    @ApiOperation("根据学院id查找系")
    @GetMapping("/findBath/{schoolid}")
    public R findBath(@PathVariable("schoolid") Integer schoolId){
        try{
            List<Department> departments=departmentService.findBatchBySchoolId(schoolId);
            return R.ok("获取成功").put("data",departments);
        }catch (Exception e){
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param department
     * @return
     */
    @ApiOperation("单个添加系部")
    @PostMapping("/addOne")
    public R addOne(@RequestBody Department department){
        try{
            if(departmentService.checkMajorIsExist(department)){
                return R.error("该系已存在");
            }else{
                departmentService.addOne(department);
                return R.ok("添加成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param schoolId
     * @return
     */
    @ApiOperation("根据学院id查找学院下的系-专业-id")
    @GetMapping("/dindDetailBatch/{schoolId}")
    public R findDetailBatch(@PathVariable("schoolId") Integer schoolId){
        try{
            return R.ok("获取成功").put("data",departmentService.findDetailBatch(schoolId));
        }catch (Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param department
     * @return
     */
    @ApiOperation("根据系的大小分页并查出下属于的专业-班级")
    @PostMapping("/findBatchByCondition")
    public R findBatchByCondition(@RequestBody Department department){
        try{
            if (department.getPageIndex() > 0) department.setPageIndex(department.getPageIndex() - 1);
            List<Department> departmentList = departmentService.findBatchByCondition(department);
            List<JSONObject> jsonObjectList = new ArrayList<>();
            for (Department department1 : departmentList) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("department", department1);
                //查处系下的专业
                List<Major> majorList = majorService.findBatch(department1.getId());
                //用于存储专业以及专业下班级
                List<JSONObject> majors = new ArrayList<>();
                for (Major major : majorList) {
                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.put("major", major);
                    List<Class> classList = classService.findBatchByMajorid(major.getId());
                    jsonObject2.put("classList", classList);
                    majors.add(jsonObject2);
                }
                jsonObject.put("majorList", majors);
                jsonObjectList.add(jsonObject);
            }
            return R.ok("获取成功").put("data",jsonObjectList).put("total", MyPageHelper.getTotal(jsonObjectList));
        }catch (Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param department
     * @return
     */
    @ApiOperation("根据id修改单条")
    @PostMapping("/modifyOneById")
    public R modifyOneById(@RequestBody Department department){
        try{
            if(checkDeleteUpdateIsSuccess(departmentService.modify(department))){
                return R.ok("修改成功");
            }else{
                return R.error("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据系id级联删除相关学生以及所有表")
    @GetMapping("/removeOneChain/{id}")
    public R removeOneChain(@PathVariable("id") Integer id){
        try{
            departmentService.removeOneChainById(id);
            return R.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月26日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据系部主键查找系")
    @GetMapping("/findOneById/{id}")
    public R findOneById(@PathVariable("id") Integer id){
        try{
            return R.ok("查找成功").put("data",departmentService.findOne(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
}
